<template>
  <div class="common-layout">
    <el-container>
      <div style="margin-left: 400px;margin-right: 200px">
        <el-main style="background-color:white;height: 600px">
          <h1> 新建仓库</h1>
          <el-form label-position="top"  label-width="90px" :rules="rules" ref="form" :model="form">
          <el-form-item label="仓库名称" style="margin-top:50px" prop="repoName">
            <el-input  v-model="form.repoName" autocomplete="off" style="width:600px "></el-input>
          </el-form-item>
            <el-form-item label="仓库介绍">
              <el-input
                  v-model="form.description"
                  maxlength="200"
                  placeholder="用简短的语言来描述下吧"
                  show-word-limit
                  type="textarea"
                  :rows="8"
                  style="width:600px"
              />
            </el-form-item>
            <div  style="height:40px;width:100px;margin-top: 5px">
              <el-radio v-model="form.isPrivate" label=false>开源(所有人可见)</el-radio>
            </div>
            <div style="height:40px;width:100px;margin-top: 5px">
              <el-radio   v-model="form.isPrivate" label=true >私有(仅仓库成员可见)</el-radio>
            </div>
            <div style="height:40px;width:100px;margin-top: 5px">
              <el-button  type="warning" style="margin-top: 20px" @click="NewRepo">创建</el-button>
            </div>
          </el-form>
        </el-main>
      </div>
    </el-container>
  </div>
</template>

<script>
import request from "@/utils/request";
import UserHomeView from "../views/user/UserHomeView"
export default {
  name: "NewRepo",
  components: {},
  data() {
    return {
      form: {
        repoId:'',
        repoName: '',
        isPrivate: '',
        description: "",
        defaultBranch:'master',
      },
      rules: {
        repoName: [
          {required: true, message: '不能为空',trigger: 'blur'},
          {min: 1, trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    NewRepo() {
      this.$refs["form"].validate((valid) => {
        this.form.ownerId=sessionStorage.getItem("userId");
        if (valid) {
          request.post("/newRepo",this.form).then(res => {
            if (res.code === 200) {
              this.$message({
                type: "success",
                message: "新建成功"
              })
              UserHomeView.methods.load()
              this.$router.push({
                path:'/'+sessionStorage.getItem("userName")
              })//跳转到登录界面
            }
            else {
              this.$message({
                type: "error",
                message: res.msg
              })
            }
          })
        }
      })
    }
  },
}
</script>
